if (typeof gxc == "undefined") {
	var gxc = {};
}

gxc.page = {};

gxc.page.create = function(param) {
	// 创建对象
	gxc.page[param.name] = {};

	// 内部别名
	var obj = gxc.page[param.name];

	// 记录对象名称
	obj.name = param.name;

	// url
	obj.url = param.url;

	// 回调函数，在reload数据后执行该函数
	if (typeof param.callback != "function") {
		obj.callback = null;
	} else {
		obj.callback = param.callback;
	}

	// loading函数，默认为null
	if (typeof param.loading != "function") {
		obj.loading = null;
	} else {
		obj.loading = param.loading;
	}

	// 排序参数
	if (typeof param.fieldSort != "string") {
		obj.fieldSort = null;
	} else {
		obj.fieldSort = param.fieldSort;
	}

	// 分页参数，默认每页20条
	if (typeof param.pageSize != "number") {
		obj.pageSize = 10;
	} else {
		obj.pageSize = param.pageSize;
	}
	obj.pageCurrent = 1;
	obj.pageCount = 0;
	obj.recordCount = 0;

	// 查询参数
	obj.params = {};

	// 设置post请求的参数
	obj.postParams = null;
	obj.setPostParams = function() {
		this.postParams = null;
		this.postParams = {};
		// 翻页参数
		this.postParams.pageSize = this.pageSize;
		this.postParams.pageCurrent = this.pageCurrent;
		this.postParams.pageCount = this.pageCount;
		this.postParams.recordCount = this.recordCount;
		// 排序参数
		if (this.fieldSort != null) {
			this.postParams.fieldSort = this.fieldSort;
		}
		// 查询参数
		for ( var item in this.params) {
			this.postParams[item] = this.params[item];
		}
	}

	// 通过ajax获取json
	obj.reload = function() {
		var theobj = this;
		theobj.setPostParams();
		jQuery.ajax({
			type : "post",
			url : theobj.url,
			data : theobj.postParams,
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
				if (theobj.loading != null) {
					theobj.loading(true);
				}
			},
			success : function(data, textStatus) {
				if (data.status == "success") {
					theobj.updatePageInfo(data.data.pageInfo);
					if (theobj.callback != null) {
						theobj.callback(data.data.list);
					}
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
				if (theobj.loading != null) {
					theobj.loading(false);
				}
			},
			error : function() {
			}
		})
	}

	// 获取pagebar
	obj.getPagebar = function() {

		var firstpage_href = " href='javascript: gxc.page." + this.name + ".firstpage()'";
		var previouspage_href = " href='javascript: gxc.page." + this.name
				+ ".previouspage()'";
		var nextpage_href = " href='javascript: gxc.page." + this.name + ".nextpage()'";
		var lastpage_href = " href='javascript: gxc.page." + this.name + ".lastpage()'";
		if (this.pageCurrent == 1 || this.pageCurrent == "1") {
			firstpage_href = "";
			previouspage_href = "";
		}
		if (this.pageCurrent == this.pageCount) {
			nextpage_href = "";
			lastpage_href = "";
		}

		// 分页工具栏
		var pagebar = "<div class='pagebar'>";
		pagebar += "<span class='pagebar_item pagebar_item_first'>第" + "<span>"
		+ this.pageCurrent + "</span>" + "页</span>";
		pagebar += "<a class='pagebar_item'" + firstpage_href
				+ ">首页</a>";
		pagebar += "<a class='pagebar_item'" + previouspage_href
				+ ">上页</a>";
		pagebar += "<a class='pagebar_item'" + nextpage_href
				+ ">下页</a>";
		pagebar += "<a class='pagebar_item'" + lastpage_href
				+ ">末页</a>";
		pagebar += "<span class='pagebar_item'>共" + "<span>"
		+ this.pageCount + "</span>" + "页</span>";
		pagebar += "</div>";

		return pagebar;
	}
	
	obj.updatePageInfo = function(pageInfo){
		this.pageCurrent = pageInfo.pageCurrent;
		this.pageCount = pageInfo.pageCount;
		this.pageSize = pageInfo.pageSize;
		this.recordCount = pageInfo.recordCount;
	}

	// 显示第一页
	obj.firstpage = function() {
		this.pageCurrent = 1;
		this.reload();
	}

	// 上一页
	obj.previouspage = function() {
		if (this.pageCurrent > 1) {
			this.pageCurrent = parseInt(this.pageCurrent) - parseInt(1);
		}
		this.reload();
	}

	// 下一页
	obj.nextpage = function() {
		if (this.pageCurrent < this.pageCount) {
			this.pageCurrent = parseInt(this.pageCurrent) + parseInt(1);
		}
		this.reload();
	}

	// 最后一页
	obj.lastpage = function() {
		this.pageCurrent = this.pageCount;
		this.reload();
	}

	return obj;
}